package com.hnepri.fang.ftx.fetch.residential;

import com.hnepri.fang.ftx.fetch.HttpUtil;
import com.hnepri.fang.ftx.model.Residential;

import java.io.IOException;
import java.util.List;

/**
 * 线程对象分区解析
 */
public class ResidentialThread extends Thread{
    private String urlPattern;
    private String path;
    private int startPage;
    private int endPage;
    private String city;


    public ResidentialThread(String urlPattern, String path, String city, int startPage, int endPage) {
        this.urlPattern = urlPattern;
        this.path = path;
        this.city = city;
        this.startPage = startPage;
        this.endPage = endPage;
    }

    @Override
    public void run() {
        for( int i = startPage; i <= endPage; i++) {
            String url = urlPattern.replace("${city}", city).replace("${page}", "1");
            String html = null;
            try {
                html = HttpUtil.getResidentialPageHtml(url);
                List<Residential> ls = ResidentialPageParser.parse(html);
                if (ls != null) {
                    ResidentialPageParser.writeResidential(path + "/page_" + i + ".txt", ls);
                }
            } catch (IOException e) {
                e.printStackTrace();
            }

        }
    }
}
